package com.kn.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kn.common.exception.UserException;
import com.kn.common.model.PageBean;
import com.kn.common.model.R;
import com.kn.common.model.RCode;
import com.kn.common.model.RPage;
import com.kn.order.dao.OrderDao;
import com.kn.order.dto.*;
import com.kn.order.mapper.*;
import com.kn.order.model.*;
import com.kn.order.model.ProductWidth;
import com.kn.order.service.OrderService;

import com.kn.order.untils.PoiExcelUtils;
import com.kn.sys.model.User;
import com.kn.sys.untils.CopyBean;
import lombok.RequiredArgsConstructor;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.unit.DataUnit;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
    private final ContractMapper contractMapper;
    private final PortMapper portMapper;
    private final CustomerMapper customerMapper;
    private final OpenScissorsMapper openScissorsMapper;
    private final PackagingMapper packagingMapper;
    private final ProductMapper productMapper;
    private final OrderDao orderDao;
    private final ProductWidthMapper productWidthMapper;
    private final TechnologyMapper technologyMapper;
    private final SimpleMapper simpleMapper;
    @Override
    public R list(PageBean pageBean, String cdate, String container, String flowerNo, Boolean isfinish) {
        PageHelper.startPage(pageBean.getPage(),pageBean.getSize());
        PageInfo<ContractDto> info = new PageInfo<>(orderDao.getOrders(cdate,container,flowerNo,isfinish));
        return R.ok(new RPage(info.getTotal(), CopyBean.copyBean(ContractDto::new,info.getList())));
    }

    @Override
    public R delete(Long id) {
        Contract contract = contractMapper.selectByPrimaryKey(id);
        contract.setId(id);
        contract.setIsdel(true);
        contractMapper.updateByPrimaryKeySelective(contract);
        return R.ok();
    }

    @Override
    public R port() {
        List<Port> ports = portMapper.selectByExample(null);
        List<PortDto> portDtos = CopyBean.copyBean(PortDto::new, ports);
        return R.ok(portDtos);
    }



    @Override
    public R add(Contract contract) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        contract.setCdate(format);
        contractMapper.insertSelective(contract);
        return R.ok();

    }

    @Override
    public R customer() {
        List<Customer> customers = customerMapper.selectByExample(null);
        List<CustomerDto> customerDtos = CopyBean.copyBean(CustomerDto::new, customers);
        return R.ok(customerDtos);
    }

    @Override
    public R openScissors() {
        List<OpenScissors> openScissors = openScissorsMapper.selectByExample(null);
        List<OpenScissorsDto> openScissorsDtos = CopyBean.copyBean(OpenScissorsDto::new, openScissors);
        return R.ok(openScissorsDtos);
    }

    @Override
    public R packaging() {
        List<Packaging> packagings = packagingMapper.selectByExample(null);
        List<PackagingDto> packagingDtos = CopyBean.copyBean(PackagingDto::new, packagings);
        return R.ok(packagingDtos);
    }

    @Override
    public R product() {
        List<Product> products = productMapper.selectByExample(null);
        List<ProductDto> productDtos = CopyBean.copyBean(ProductDto::new, products);
        return R.ok(productDtos);
    }

    @Override
    public R technology () {
        List<Technology> technologies = technologyMapper.selectByExample(null);
        List<TechnologyDto> technologyDtos = CopyBean.copyBean(TechnologyDto::new, technologies);
        return R.ok(technologyDtos);
    }

    @Override
    public R simple() {
        List<Simple> simples = simpleMapper.selectByExample(null);
        List<SimpleDto> simpleDtos = CopyBean.copyBean(SimpleDto::new, simples);
        return R.ok(simpleDtos);
    }

    @Override
    public R orderById(Long id) {
        Contract contract = contractMapper.selectByPrimaryKey(id);

        return R.ok(contract);
    }

    @Override
    public R edit(Long id, Contract contract) {
        Contract contract1 = contractMapper.selectByPrimaryKey(id);
        if(contract1==null){
            throw new UserException(RCode.NOT_EXIST_USER);
        }
        contractMapper.updateByPrimaryKeySelective(contract);
        return R.ok();
    }

    @Override
    public R exportAddresses(HttpServletResponse response) {
        // 数据库表对应的字段
        String[] titles = new String[] {"日期","柜号","花号","坯布米数","客户","码口","幅宽","开剪码数","成件码数","样品","样品数量","坯布克重","成品克重"};
        List<Map<String,Object>> objList = new ArrayList<>();
        List<ContractDto> orderss = orderDao.getOrder();
        // 数据库表对应的数据
        List<ContractDto>  orders= PoiExcelUtils.getMeetEnrollList(orderss);
        for(ContractDto order : orders){
            Map<String,Object> tempMap = new HashMap<>();
            tempMap.put("日期", order.getCdate());
            tempMap.put("柜号", order.getContainer());
            tempMap.put("花号", order.getFlowerNo());
            tempMap.put("坯布米数", order.getCnumber());
            tempMap.put("客户", order.getCname());
            tempMap.put("码口", order.getCodePort());
            tempMap.put("开剪码数", order.getOpenScissors());
            tempMap.put("成件码数", order.getProduct());
            tempMap.put("成品克重", order.getProductWeight());
            tempMap.put("样品", order.getScname());
            tempMap.put("样品数量", order.getClength());
            tempMap.put("坯布克重", order.getGreyWeight());
            tempMap.put("幅宽", order.getProductWidth());
            objList.add(tempMap);
        }
        String path = PoiExcelUtils.exportExcel("订单",titles,objList);
        response.setHeader("Content-disposition", "attachment;filename=" + path + ".xlsx");
        if(path == null){
            return R.error("导出失败！");
        }
        return R.ok(RCode.SUCCESS_DOWN);
    }

    @Override
    public R export() {
        List<ContractDto> orderss = orderDao.getOrder();
        return R.ok(orderss);
    }

    @Override
    public R productWidth () {
        List<ProductWidth> productWidths = productWidthMapper.selectByExample(null);
        List<ProductWidthDto> productWidthDtos = CopyBean.copyBean(ProductWidthDto::new, productWidths);
        return R.ok(productWidthDtos);
    }

}
